package com.stu.day13.listsort;

import java.util.*;

/**
 * @author zy
 * @date 2025/2/11 16:51
 */
public class ListSortDemo2 {
    public static void main(String[] args) {
        Order2 o1 = new Order2(1,"1001",100.0d);
        Order2 o2 = new Order2(2,"1002",200.0d);
        Order2 o3 = new Order2(3,"1003",100.0d);
        Order2 o4 = new Order2(4,"1004",400.0d);
        Order2 o5 = new Order2(5,"1005",500.0d);

        List<Order2> list = new ArrayList<>();
        list.add(o1);
        list.add(o2);
        list.add(o3);
        list.add(o4);
        list.add(o5);

        //TODO Collections工具类中的sort方法
        Collections.sort(list,new Comparator<Order2>(){
            @Override
            // 内部类重写compare,如果Order2实现Comparable<Order2>接口则重写compareTo
            public int compare(Order2 or1,Order2 or2){
//                if(or1.getTotal()>or2.getTotal())
//                    return 1;
//                else if (or1.getTotal()< or2.getTotal()) {
//                    return -1;
//                }
//                return 0;
                return (int)(or1.getTotal()-or2.getTotal());
            }
        });
        //Collections.sort(list,(or1,or2)->(int)(or1.getTotal()-or2.getTotal()));

        //TODO jdk8.0开始List中自己就提供好了sort方法
        //list.sort((m1,m2)->(int)(m2.getTotal()-m1.getTotal()));

        Iterator<Order2> iterator = list.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

class Order2{
    private Integer id;
    private String ordNO;
    private Double total;

    public Order2() {
    }

    public Order2(Integer id, String ordNO, Double total) {
        this.id = id;
        this.ordNO = ordNO;
        this.total = total;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getOrdNO() {
        return ordNO;
    }

    public void setOrdNO(String ordNO) {
        this.ordNO = ordNO;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Order2 order = (Order2) o;
        return Objects.equals(id, order.id) && Objects.equals(ordNO, order.ordNO);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, ordNO);
    }

    @Override
    public String toString() {
        String sb = "Order{" + "id=" + id +
                ", ordNO='" + ordNO + '\'' +
                ", total=" + total +
                '}';
        return sb;
    }
}
